package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowParticipantInfoVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowParticipantRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowParticipantInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowParticipantInfoExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowParticipantInfoDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-02-19 21:35
 */
@Service
public class FlowParticipantRepositoryImpl implements FlowParticipantRepository {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FlowParticipantInfoDAO flowParticipantInfoDAO;

    @Override
    public int insertParticipant(FlowParticipantInfoVo entity) {
        if (Objects.isNull(entity)) {
            return 0;
        }
        if (Objects.nonNull(findByParticipantId(entity.getParticipantId()))) {
            logger.info("参与者{}已经存在，不用重新插入", entity.getParticipantId());
            //已经存在,则直接返回
            return 1;
        }
        return flowParticipantInfoDAO.insertSelective(convert(entity));
    }

    @Override
    public FlowParticipantInfoVo findByParticipantId(String participantId) {
        if (StringUtils.isBlank(participantId)) {
            logger.warn("传递的participantId值为空");
            return null;
        }
        FlowParticipantInfoExample example = new FlowParticipantInfoExample();
        example.createCriteria().andParticipantIdEqualTo(participantId);
        List<FlowParticipantInfoVo> flowParticipantInfos = listByExample(example);
        if (CollectionUtils.isEmpty(flowParticipantInfos)) {
            logger.warn("传递的participantId值为{},没有对应的记录", participantId);
            return null;
        }
        return flowParticipantInfos.get(0);
    }

    @Override
    public List<FlowParticipantInfoVo> listByParticipantIds(List<String> candidateGroups) {
        FlowParticipantInfoExample example = new FlowParticipantInfoExample();
        example.createCriteria().andParticipantIdIn(candidateGroups);
        return listByExample(example);
    }

    @Override
    public int insertParticipantInfo(FlowParticipantInfoVo entity) {
        if (Objects.isNull(entity)) {
            return 0;
        }
        List<String> participantTypeList = new ArrayList<>();
        participantTypeList.add(entity.getParticipantType());
        if (Objects.nonNull(findByParticipantIdAndType(entity.getParticipantId(), participantTypeList))) {
            logger.info("参与者{}已经存在，不用重新插入", entity.getParticipantId());
            //已经存在,则直接返回
            return 1;
        }
        return flowParticipantInfoDAO.insertSelective(convert(entity));
    }

    @Override
    public List<FlowParticipantInfoVo> findByParticipantIdAndType(String participantId, List<String> participantTypeList) {
        if (StringUtils.isBlank(participantId)) {
            logger.warn("传递的participantId值为空");
            return null;
        }
        if (CollectionUtils.isEmpty(participantTypeList)) {
            logger.warn("传递的participantTypeList值为空");
            return null;
        }
        FlowParticipantInfoExample example = new FlowParticipantInfoExample();
        FlowParticipantInfoExample.Criteria criteria = example.createCriteria();
        criteria.andParticipantIdEqualTo(participantId).andParticipantTypeIn(participantTypeList);
        List<FlowParticipantInfoVo> flowParticipantInfos = listByExample(example);
        if (CollectionUtils.isEmpty(flowParticipantInfos)) {
            logger.warn("传递的participantId值为{},没有对应的记录", participantId);
            return null;
        }
        return flowParticipantInfos;
    }

    @Override
    public FlowParticipantInfoVo findByIdAndType(String participantId, String type) {
        if (StringUtils.isBlank(participantId)) {
            logger.warn("传递的participantId值为空");
            return null;
        }
        if (StringUtils.isBlank(type)) {
            logger.warn("传递的participantType值为空");
            return null;
        }

        FlowParticipantInfoExample example = new FlowParticipantInfoExample();
        FlowParticipantInfoExample.Criteria criteria = example.createCriteria();
        criteria.andParticipantIdEqualTo(participantId).andParticipantTypeEqualTo(type);
        List<FlowParticipantInfoVo> flowParticipantInfos = listByExample(example);
        if (CollectionUtils.isNotEmpty(flowParticipantInfos)){
            return flowParticipantInfos.get(0);
        }
        return null;
    }

    public List<FlowParticipantInfoVo> listByExample(FlowParticipantInfoExample example) {
        return flowParticipantInfoDAO.selectByExample(example).stream().map(this::convert).collect(Collectors.toList());
    }

    private FlowParticipantInfoVo convert(FlowParticipantInfo entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowParticipantInfoVo result = new FlowParticipantInfoVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }

    private FlowParticipantInfo convert(FlowParticipantInfoVo entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowParticipantInfo result = new FlowParticipantInfo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
